import React, { useRef } from 'react'
import { useHistory } from 'umi';

export default function Index() {
    var history = useHistory();
    const name = useRef(null)
    function login() {
        const inputValue = name.current.value
        if (inputValue) {
            console.log("点击的登陆", name.current.value);
            fetch("/user/login", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({
                    name: inputValue,
                })
            }).then(res => res.json()).then(
                res => {
                    console.log(res)
                    if (res.ok) {
                        localStorage.setItem("token", inputValue);
                        history.push("/center");
                    } else {
                        alert("用户名输入错误,试试输入zzy")

                    }
                }
            )

        } else {
            alert("请输入用户名称。")
        }


    }
    return (
        <div style={{ width: "100%" }}>
            <input type="text" ref={name}></input>
            <button onClick={login}>登陆</button>
        </div >
    )
}
